<table class="am-table">
<tr>
    <td width="400px">
        <table class="form-table-without-line">
            <tr>
                <th>期号</th>
                <td>
                    <input type="text" class="am-form-field issue" placeholder="输入待设置的期数" />
                    <button type="button" class="see">查看</button>
                </td>
            </tr>
            <tr>
                <th>开奖结果</th>
                <td>
                    <input type="text" class="am-form-field result" placeholder="10个开奖号码，请用,隔开" />
                    <button type="button" class="save">保存</button>
                </td>
            </tr>

            <tr>
                <th>杀率模式</th>
                <td>
                    <select class="mode">
                        <option value="-1">无</option>
                        <option value="1">针对所有用户</option>
                        <option value="2">针对部分用户</option>
                    </select>
                    <button type="button" class="mode-save">保存</button>
                </td>
            </tr>
            
            <tr>
                <th>全局杀率</th>
                <td>
                    <input type="number" class="am-form-field global-rate" placeholder="例如：0.8表示80%概率杀" />
                    <button type="button" class="global-rate-save">保存</button>
                </td>
            </tr>
            
            <tr>
                <th>用户杀率</th>
                <td>
                    <input type="number" class="am-form-field user-rate" placeholder="例如：0.8表示80%概率杀" />
                    <textarea class="user-list" rows="10" placeholder="请输入起效的用户列表，用,分割"></textarea>
                    <button type="button" class="user-rate-save">保存</button>
                </td>
            </tr>
        </table>
    </td>
    <td style="padding: 0px 0px 0px 20px;">
        <div class="currissue" style="font-size: 18px; color:red;">期号 </div>
        <div class="countdown" style="font-size: 18px; color:red;">开奖倒计时 00:00:00</div>

        <button class="btn-refersh">更新下注信息</button>
        <div class="kill">
            <button class="one-numbers">生成号码</button>
            <input type="hidden" class="one-numbers">
            <button class="one-kill">杀</button>
            <table class="am-table one-result">
                <thead>
                    <tr>
                        <th>冠军</th>
                        <th>亚军</th>
                        <th>三</th>
                        <th>四</th>
                        <th>五</th>
                        <th>六</th>
                        <th>七</th>
                        <th>八</th>
                        <th>九</th>
                        <th>十</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table> 
        </div>

        <div class="b-title">本期部分用户下注统计</div>
        <table class="am-table one-bets">
            <thead><tr>
                <th>类型</th>
                <th>可中奖</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </td>
    <td style="padding: 0px 0px 0px 20px;">
        <div class="kill">
            <button class="all-numbers">生成号码</button>
            <input type="hidden" class="all-numbers">
            <button class="all-kill">杀</button>
            <table class="am-table all-result">
                <thead>
                    <tr>
                        <th>冠军</th>
                        <th>亚军</th>
                        <th>三</th>
                        <th>四</th>
                        <th>五</th>
                        <th>六</th>
                        <th>七</th>
                        <th>八</th>
                        <th>九</th>
                        <th>十</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table> 
        </div>

        <div class="b-title">本期全局下注统计</div>
        <table class="am-table all-bets">
            <thead>
                <tr>
                <th>类型</th>
                <th>可中奖</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </td>
</tr>
</table>
<style type="text/css">
    .b-title {
        line-height: 40px;
        padding-left: 20px;
        color: blue;
        background-color: #ccc;
        margin-top: 20px;
    }
    .btn-refersh {
        line-height: 40px;
        margin: 10px 0px 10px 0px;
    }
    .kill {
        padding: 10px;
    }
    .kill button {
        line-height: 40px;
        min-width: 80px;
        margin-top: 10px;
    }
    .kill input {
        line-height: 40px;
        margin-top: 10px;
        width: 200px;
    }
</style>
<script src="../public/all/js/result.js"></script>
<script>
    // 将最近一期的期号和开奖时间填入
    var game = 'zypk10';
    Result.switchGame(game);
    Result.cb = function() {
        var curr = Result.curr();
        if (!curr) return;

        $(".issue").val(curr.issue);
        updateIssueResult(curr.issue);
    };

    // 更新某一期的设置结果
    function updateIssueResult(issue) {
        CMD.query_zy_lottery_result(function(r) {
            for (var i in r.list) {
                if (r.list[i].issue == issue) {
                    // 数据填入
                    fill_results(r.list[i].red.split(","));
                }
            }
        });
    }

    $(".see").click(function() {
        var issue = $(".issue").val().trim();
        if (issue == '') return;
        updateIssueResult(issue);
    });

    function fill_results(list) {
        $("input.result").val(list.join(","));
    }

    $("button.save").click(function() {
        var arr = $("input.result").val().trim().split(",");
        if (arr.length != 10) {
            alert2("开奖号码应该为10个！", true);
            return;
        }

        for (var i = 0; i < arr.length; i++) {
            var n = arr[i];
            if (n == '' || parseInt(n) < 1 || parseInt(n) > 10) {
                alert2("开奖结果数字应该为1-10。", true);
                return;
            }
            n = parseInt(n);
            arr[i] = n;

            // 确保数字不重复
            for (var j = 0; j < i; j++) {
                if (arr[j] == arr[i]) {
                    alert2("开奖数字不能重复！", true);
                    return;
                }
            }
        } 

        // 数字处理下（前加0）
        var arr2 = [];
        for (var i in arr) {
            arr2.push(arr[i] >= 10 ? "" + arr[i]: "0" + arr[i]);
        }

        // 保存
        startLoading();
        CMD.set_zy_lottery_result(game, $(".issue").val().trim(), arr2, function() {
            stopLoading();
            alert2("设置成功！");
        });
    });

    // 杀率的设置
    lottery_ai(game);
</script>
<script>
    // 获取当前期号
    var currIssue;
    function loopNext() {
        var type = 'zypk10';
        CMD.lottery_next(type, function(r) {
            currIssue = r.issue;
            $(".currissue").html("期号 " + currIssue);
            var delta = new Date(r.open).getTime() - new Date().getTime();
            delta /= 1000;

            var h = parseInt(delta / 3600);
            var m = parseInt((delta - h*3600) / 60);
            var s = parseInt(delta - h * 3600 - m * 60);
            $(".countdown").html("开奖倒计时 " + h + ":" + m + ":" + s);
        });
    }
    setInterval(function() {
        loopNext();
    }, 5000);
    loopNext();

    var _queryBets = function() {
        if (!currIssue) return;
        startLoading();
        CMD.query_bet("zypk10", "", {
            pageNum: 1,
            pageSize: 5000,
            issues: currIssue + "," + currIssue,
        }, function(r) {
            stopLoading();
            statBets(r);
        });
    }
    $(".btn-refersh").click(function() {
        _queryBets();
    });

    var listAll = {}, listOne = {};
    function statBets(r) {
        var type = 'zypk10';
        var users = $(".user-list").val().trim().split(",");
        listAll = {}; 
        listOne = {};
        for (var i in r.list) {
            var d = r.list[i];
            if (!listAll[d.target]) listAll[d.target] = 0;
            listAll[d.target] += d.amount * d.rate;

            if (users.indexOf(d.user.mobile) == -1) continue;
            if (!listOne[d.target]) listOne[d.target] = 0;
            listOne[d.target] += d.amount * d.rate;
        }

        var list1 = [], list2 = [];
        for (var k in listAll) {
            list1.push({
                target: k,
                amount: listAll[k]
            });
        }
        list1.sort(function(a, b) {
            return b.amount - a.amount;
        });

        for (var k in listOne) {
            list2.push({
                target: k,
                amount: listAll[k]
            });
        }
        list2.sort(function(a, b) {
            return b.amount - a.amount;
        });

        var table = $(".one-bets tbody"); table.empty();
        for (var i in list2) {
            var d = list2[i];
            var tr = $("<tr></tr>");
            var target = Util.convertTarget(type, d.target);
            tr.append("<td>" + target + "</td>");
            tr.append("<td>" + parseInt(d.amount / 100) + "</td>");
            table.append(tr);
        }

        var table = $(".all-bets tbody"); table.empty();
        for (var i in list1) {
            var d = list1[i];
            var tr = $("<tr></tr>");
            var target = Util.convertTarget(type, d.target);
            tr.append("<td>" + target + "</td>");
            tr.append("<td>" + parseInt(d.amount / 100) + "</td>");
            table.append(tr);
        }
    }

    $(".one-numbers").click(function() {
        if (Util.isEmpty(listOne)) {
            alert2("没有投注记录，尝试刷新下注单", true);
            return;
        }

        var arr = Pk10Ai.open(listOne, Zypk10Rule.map, 1);
        $("input.one-numbers").val(arr.join(","));
        var table = $(".one-result tbody"); table.empty();
        var tr = $("<tr></tr>"); table.append(tr);
        for (var i in arr) {
            tr.append("<td>" + arr[i] + "</td>");
        }
    });
    $(".one-kill").click(function() {
        var v = $("input.one-numbers").val();
        if (!v) return;
        var result = v.split(",");
        if (result.length != 10) return;

        // 设置开奖结果
        CMD.set_zy_lottery_result("zypk10", currIssue, result, function() {
            alert2("设置成功！");

            $("input.issue").val(currIssue);
        });
    });

    $(".all-numbers").click(function() {
        if (Util.isEmpty(listAll)) {
            alert2("没有投注记录，尝试刷新下注单", true);
            return;
        }

        var arr = Pk10Ai.open(listAll, Zypk10Rule.map, 1);
        $("input.all-numbers").val(arr.join(","));
        var table = $(".all-result tbody"); table.empty();
        var tr = $("<tr></tr>"); table.append(tr);
        for (var i in arr) {
            tr.append("<td>" + arr[i] + "</td>");
        }
    });
    $(".all-kill").click(function() {
        var v = $("input.all-numbers").val();
        if (!v) return;
        var result = v.split(",");
        if (result.length != 10) return;

        // 设置开奖结果
        CMD.set_zy_lottery_result("zypk10", currIssue, result, function() {
            alert2("设置成功！");

            $("input.issue").val(currIssue);
        });
    });
</script>
